package order

import (
	"errors"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"higame/modules/store"
	"higame/modules/utils"
	"net/http"
)

func list(ctx *gin.Context) {
	orderType := ctx.Query("type")
	if orderType == "goods" {
		orderType = string(Goods)
	} else if orderType == "game" {
		orderType = string(Game)
	} else {
		ctx.JSON(400, gin.H{
			"code": 400,
			"msg":  "参数出错",
			"data": nil,
		})
		return
	}

	where := bson.M{"order_type": orderType}
	filterType := ctx.PostForm("filter_type")
	filterText := ctx.PostForm("filter_text")
	pageNum := utils.StringToInt64(ctx.PostForm("page_num"), 1)
	if filterType != "" {
		where[filterType] = bson.M{
			"$regex":   filterText,
			"$options": "$i",
		}
	}

	// 查询订单
	orders, err := FindMany(pageNum, where)
	if err != nil {
		ctx.JSON(400, gin.H{
			"code": 400,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	count, _ := Count(where)
	str := ""
	if count == 0 {
		str = "没有查询到数据！"
	} else {
		str = utils.JoinString("共%v页%v行 当前在第%v页", count/20+1, count, pageNum)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  str,
		"data": gin.H{
			"list":  orders,
			"total": count,
		},
	})
}

func myList(ctx *gin.Context) {
	orderType := ctx.Query("type")
	who := ctx.Query("who")
	if orderType == "goods" {
		orderType = string(Goods)
	} else if orderType == "game" {
		orderType = string(Game)
	} else {
		ctx.JSON(400, gin.H{
			"code": 400,
			"msg":  "参数出错",
			"data": nil,
		})
		return
	}
	claims, _ := store.GetClaims(ctx)
	where := bson.M{
		"order_type": orderType,
	}
	if who == "seller" {
		where["seller"] = claims.Audience
	} else if who == "buyer" {
		where["buyer"] = claims.Audience
	} else {
		ctx.JSON(400, gin.H{
			"code": 400,
			"msg":  "参数出错",
			"data": nil,
		})
		return
	}
	filterType := ctx.PostForm("filter_type")
	filterText := ctx.PostForm("filter_text")
	pageNum := utils.StringToInt64(ctx.PostForm("page_num"), 1)
	// 非管理员只能查询自己的订单
	if filterType != "" && filterType != "seller" && filterType != "buyer" {
		where[filterType] = bson.M{
			"$regex":   filterText,
			"$options": "$i",
		}
	}

	// 查询订单
	orders, err := FindMany(pageNum, where)
	if err != nil {
		ctx.JSON(400, gin.H{
			"code": 400,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	count, _ := Count(where)
	str := ""
	if count == 0 {
		str = "没有查询到数据！"
	} else {
		str = utils.JoinString("共%v页%v行 当前在第%v页", count/20+1, count, pageNum)
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  str,
		"data": gin.H{
			"list":  orders,
			"total": count,
		},
	})
}

func delete(ctx *gin.Context) {
	orderId := ctx.PostForm("order_id")
	if err := DeleteOneByOrderID(orderId); err != nil {
		ctx.JSON(400, gin.H{
			"code": 400,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
		"data": nil,
	})
}

func do(ctx *gin.Context) {
	action := Type(ctx.Query("action"))
	var err error = nil
	switch action {
	case "FH":
		err = handleFH(ctx)
		break
	case "SH":
		err = handleSH(ctx)
		break
	case "QX":
		err = handleQX(ctx)
		break
	default:
		err = errors.New("参数出错")
		break
	}

	if err != nil {
		ctx.JSON(400, gin.H{
			"code": 400,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "操作成功",
		"data": nil,
	})
}
